<?php

defined('BASEPATH') or die('forbidden');

class SealAccountList_Controller Extends Controller {

    private $wholeServerHost;	//全局地址
    private $areaList;	//区号列表 
    private $sealAccountDesribeList;	//封角色描述列表
    private $type;
	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $this->type = array(
            '0' => translate('SEAL_TYPE_ACC_LOGIN_DISABLE'),
            '1' => translate('SEAL_TYPE_ROLE_LOGIN_DISABLE'),
            '2' => translate('SEAL_TYPE_ACC_CHAT_DISABLE'),
            '3' => translate('SEAL_TYPE_ROLE_CHAT_DISABLE'),
            '4' => translate('SEAL_TYPE_ACC_LOGIN_ENABLE'),
            '5' => translate('SEAL_TYPE_ROLE_LOGIN_ENABLE'),
            '6' => translate('SEAL_TYPE_ACC_CHAT_ENABLE'),
            '7' => translate('SEAL_TYPE_ROLE_CHAT_ENABLE'),
        );

        $config = get_config();        
        $this->wholeServerHost = $config['host_url'];
    }

    function index() {
        $this->view->display("seal/sealaccountlist/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $post_data['SessionID']="ewewqw";
        $post_data['SessionKey']="wqqew";

        $strResult=vpost($this->wholeServerHost.'gm_get_dis_acc_lst',3,json_encode($post_data));
        $response=json_decode($strResult,true);

        if ($response['RetCode'] == 0) {
            foreach ($response['DisAccInfos'] as $disAccInfo){
                $value['account_id'] =$disAccInfo['AccountID'];
                $value['role_id'] = $disAccInfo['PlayerID'];
                $value['type'] = $this->type [strval($disAccInfo['Type'])];
                $value['begin_time'] =$disAccInfo['BeginTime'];
                $value['disable_days'] =$disAccInfo['DisableDay'];
                $value['des_info'] = $disAccInfo['Desc'];

                $rows[] = array('id' => $disAccInfo['AccountID'], 'cell' => $value);
            }

            $total = count($rows);
            $result = array(
                'page' => $page,
                'total' => $total,
                'rows' => $rows
            );

        //    echo "get seal account success ";
        //    cp_tips("success");
        } else {
            echo " get seal account error ";
        }

        echo json_encode($result);
    }

    function add() {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $role_id_name_str = (string)$rowdata['roleidname'];

            if ($role_id_name_str == ""){
                echo "<script>alert('用户名和玩家ID至少要填一个!!!')</script>";
                return;
            }

            $isnum = is_numeric($role_id_name_str);
            if ( false == $isnum ){ // 输入角色名，需要查找玩家ID和账号
                $post_data['Name']=$role_id_name_str;
                $post_url = $this->wholeServerHost.'gm_get_playerid_by_name';
                $re = vpost($post_url,3,json_encode($post_data));
                if ( $re ) {
                    $res=json_decode($re,true);
                    $account = (int)$res['AccountID'];
                    $playerid = $res['PlayerID'];
                    $areanum = $res['SvrID'];
                } else {
                    $result = translate ( 'C_TIPS_ERROR_TEXT' );
                    error ( $result );
                }
            }else{ //直接输入的玩家ID得到账号和区号
                $len = strlen($role_id_name_str);
                $area_num_str = substr($role_id_name_str, $len-6, 3);
                $acc_str = substr($role_id_name_str, 0, $len-6);
                $account = (int)$acc_str;
                $areanum = (int)$area_num_str;
                $playerid = $role_id_name_str; //(int)$role_id_name_str;
            }

            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";
            $post_data['PlayerID'] = (String)$playerid; //(int)$playerid;
            $post_data['AccountID']=$account;
            $post_data['SvrID']=(int)$areanum;
            $post_data['RoleName']=(string)$rowdata['role_name'];
            $post_data['Enable']=(int)$rowdata['enable'];
            $post_data['DisableDay']=(int)$rowdata['seal_time'];
            $post_data['Desc']=$rowdata['description'];

            $strResult=vpost($this->wholeServerHost.'gm_enable_account',3,json_encode($post_data));
            $result=json_decode($strResult,true);


            //操作失败或者连接失败
            if ($result['RetCode'] === 0) {
                insert_log(translate('MENU_SEAL_SEALACCOUNTLIST'),translate('ADD'), NULL, $rowdata);
                //$this->db->insert("gm_sealaccount",array('description'=>$rowdata['description'],'accountid'=>$rowdata['accountid'],
                //   'areanum'=>$rowdata['area'],'rolename'=>$rowdata['role_name']));

                $this->db->insert("gm_sealaccount",array('description'=>$rowdata['description'],'accountid'=>$rowdata['accountid'],
                   'areanum'=>$areanum,'rolename'=>$rowdata['role_name']));

                echo " seal account success ";
                cp_tips("success");
            } else {
                echo " connct error ";
            }
        }else{
            $time = array("86400" => translate('ONE_DAY'), "172800" => translate('TWO_DAYS'), "604800" => translate('ONE_WEEK'), "0" => translate('PERPETUAL'));
            $this->view->assign("timelist", $this->form->select("row[seal_time]", $time, '', array('id' => 'seal_time')));
            //$this->view->assign("arealist", $this->areaList);
            $this->view->display("seal/sealaccountlist/add");
        }
    }

    function del($id = 0) {
        $post_data['ssionID']='';
        $strResult=vpost($this->wholeServerHost.'gm_enable_account',3,json_encode($post_data));
        $str = $this->wholeServerHost . "action=14&id={$id}";

        $strResult = vpost($str);

        $res = (int) $strResult == 0 ? 1 : 0;
        if ($res == 1) {
            insert_log(translate('MENU_SEAL_SEALACCOUNTLIST'),translate('DEL'), $id);
        }
        json_output($res, translate('C_TIPS_ERROR_TEXT'));
    }

    //通过区号与角色名得到账号id
    function mygetAccountId($areanum, $rolename)
    {
        $post_url='';
        $server_list=get_server_list();
        foreach ($server_list as $k=>$v){
            if($v['SvrID'] == $areanum){
                $post_url=$v['SvrOutAddr'];
            }
        }
        $post_data='{"Name":"'.$rolename.'"}';
        $row = vpost($post_url.'/query_account_id',3,$post_data);
        $rows=json_decode($row,true);
        return $rows;
    }
}